<?php

namespace addons\wleaflet\controller;

use think\addons\Controller;

class Index extends Controller
{

    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }


    /**
     * @name is_https 判断是否是https
     * @return bool
     */
    public static function is_https() {
        if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
            return true;
        } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }
        return false;
    }

    /**
     * 选择地址
     */
    public function select()
    {
        $getParams = $this->request->get();
        $this->assign('sel_type',($getParams['sel_type']??'address'));
        $getParams['latlng'] = !empty($getParams['latlng'])?htmlspecialchars_decode($getParams['latlng']):'';
        $this->assign('getParams',$getParams);
        $this->assign('isHttps',self::is_https());
        return $this->view->fetch();
    }


    /**
     * @param bool $returnArr
     * 接口传输数据。当为其他方法调用的时候返回数组
     * 当为接口请求获取数据的时候，返回JSON格式数据
     */
    public function getConfig()
    {
        $params = get_addon_config('wleaflet');
        $defaultLayer = $params['defaultLayer']??'';
        $useDefaultLayerList = !empty($params['useDefaultLayerList'])?explode(',',$params['useDefaultLayerList']):[];
        $params['useDefaultLayerList'] = array_merge($useDefaultLayerList,[$defaultLayer]);
        $useDefaultLayerList = array_unique(array_filter($params['useDefaultLayerList']));
        $useDefaultLayerList = $useDefaultLayerList?array_values($useDefaultLayerList):[];
        $setConfig = [
            'defaultLayer' => $defaultLayer,
            'useDefaultLayerList' => $useDefaultLayerList??[],
            'defaultCenter' =>  [$params['lat'],$params['lng']],
            'zoomBase' => $params['zoomBase']??'',
            'gaodeKey' => $params['gaodeKey']??'',
            'gaodeSecurityJsCode' => $params['gaodeSecurityJsCode']??'',
            'tdtKey' => $params['tdtKey']??'',
            'tencentKey' => $params['tencentKey']??'',
            'baiduKey' => $params['baiduKey']??'',
        ];
        return json([
            'config'=>$setConfig
        ]);
    }


}
